package com.xindaoapp.happypet.ui;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.adapter.PostListAdapter;
import com.xindaoapp.happypet.application.HappyPetApplication;
import com.xindaoapp.happypet.bean.MainZuixinData;
import com.xindaoapp.happypet.bean.MainZuixinData.MainTiezi;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;

/**
 * 首页-- 标签
 * 
 * @author
 * 
 */
public class MyTagListActivity extends BaseActivity {

    private PullToRefreshListView mPullToRefreshListView;
    private MyTagAdapter mAdapter;
    private RelativeLayout mNoData;
    private Context mContext;
    private List<MainTiezi> mList;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_my_tag_list;
    }

    @Override
    protected void initViews() {
        mContext = this;

        mNoData = (RelativeLayout) this.findViewById(R.id.no_data);

        mPullToRefreshListView = (PullToRefreshListView) this.findViewById(R.id.listview);
        mPullToRefreshListView.setMode(Mode.PULL_FROM_START);
        mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {

            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                onLoadDatas();
            }
        });


        super.initViews();
    }



    @Override
    protected void initEvents() {
        super.initEvents();
    }

    @Override
    protected void onLoadDatas() {
        super.onLoadDatas();
        getData();
    }



    private void getData() {

        getMoccaApi().getTagListData(getIntent().getStringExtra(Constants.KEY_TAGID),
                Constants.PAGE_FIRST, Constants.PAGE_SIZE, new IRequest<MainZuixinData>() {

                    @Override
                    public void request(MainZuixinData t) {
                        // TODO Auto-generated method stub
                        if (t != null) {
                            if (t.array.size() == 0) {
                                mNoData.setVisibility(View.VISIBLE);
                            } else {
                                onDataArrived(true);

                                mNoData.setVisibility(View.GONE);
                                mAdapter =
                                        new MyTagAdapter(MyTagListActivity.this, t.array,
                                                Constants.PAGE_SIZE, R.layout.item_zuixinfragment,
                                                R.layout.item_loading);
                                mPullToRefreshListView.getRefreshableView().setAdapter(mAdapter);
                            }

                        } else {
                            onDataArrived(false);
                        }
                        mPullToRefreshListView.onRefreshComplete();
                    }
                });


    }

    @Override
    protected OnClickListener getLeftViewClickListener() {
        return new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                finish();
            }
        };
    }


    @Override
    protected int getTopBarLeftImageViewRes() {
        return R.drawable.top_bar_back;
    }


    @Override
    protected String getTopBarTitleStrRes() {
        return getIntent().getStringExtra(Constants.KEY_GROUP_NAME);
    }

    class MyTagAdapter extends PostListAdapter {

        public MyTagAdapter(Context context, List<MainTiezi> iniData, int pageSize, int res,
                int loadingRes) {
            super(context, iniData, pageSize, res, loadingRes);
            // TODO Auto-generated constructor stub
        }

        @Override
        protected List<MainTiezi> parseResult(List<MainTiezi> list) {
            // TODO Auto-generated method stub
            for (int i = list.size() - 1; i >= 0; i--) {
                if (mAdapter.getResult().contains(list.get(i))) {
                    list.remove(list.get(i));
                }
            }
            return list;
        }

        @Override
        public void nextPage(
                int start,
                int size,
                final com.xindaoapp.happypet.adapter.XinDaoBaseAdapter.ILoadNextPageData<MainTiezi> iLoadNextPageData) {
            // TODO Auto-generated method stub
            super.nextPage(start, size, iLoadNextPageData);

            HappyPetApplication
                    .get()
                    .getMoccaApi()
                    .getTagListData(getIntent().getStringExtra(Constants.KEY_TAGID), start, size,
                            new IRequest<MainZuixinData>() {

                                @Override
                                public void request(MainZuixinData t) {
                                    iLoadNextPageData.loadNextPageData(t == null ? null : t.array);

                                }
                            });

        }
    }

}
